﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeShop.Infrastructure.Entities.Repository;
using System.Drawing;
using System.IO;
using CollegeShop.Tool.Framework;

namespace CollegeShop.Web.Controllers
{
    public class SysController : Controller
    {
        //
        // GET: /Sys/

        public void Resource(int id)
        {
            try
            {
                var resource = new Resource().Find(id.ToString());
                if (resource != null)
                {
                    string path = this.HttpContext.Server.MapPath(resource.Path);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                        Bitmap bmap = new Bitmap(path);
                        bmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        this.HttpContext.Response.ClearContent();
                        this.HttpContext.Response.ContentType = "image/Png";
                        this.HttpContext.Response.BinaryWrite(memoryStream.GetBuffer());

                        memoryStream.Close();
                        memoryStream = null;
                        bmap.Dispose();
                        bmap = null;
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
        }

    }
}
